/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UDPFiles;

import Tool.FileCodeMaker;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONObject;

/**
 * UDP指令实体类
 * @author wcss
 */
public class UDPDataCmd 
{
    private String cmd;
    private String totalCode;
    private String subCode;
    private long dataIndex;
    private String data;
    
    public UDPDataCmd(String cd,String tcode,String scode,long dindex,String dat)
    {
        this.cmd = cd;
        this.totalCode = tcode;
        this.subCode = scode;
        this.dataIndex = dindex;
        this.data = dat;
    }
    
    /**
     * 生成控制指令
     * @param cmd
     * @param totalcode
     * @return 
     */
    public static UDPDataCmd makeControlCmd(String cmd,String totalcode)
    {
        return new UDPDataCmd(cmd,totalcode,"",0,"");
    }
    

    /**
     * 生成控制指令
     * @param cmd
     * @param totalcode
     * @param dindex
     * @return 
     */
    public static UDPDataCmd makeControlCmd(String cmd,String totalcode,long dindex)
    {
        return new UDPDataCmd(cmd,totalcode,"",dindex,"");
    }
    

    /**
     * 生成文件指令
     * @param cmd
     * @param totalcode
     * @param dindex
     * @param dat
     * @return 
     */
    public static UDPDataCmd makeFileCmd(String cmd,String totalcode,long dindex,byte[] dat) throws Exception
    {
        String scode = FileCodeMaker.makeBytesCode(dat);
        String datas = Base64Tool.encodeToBase64(dat);
        return new UDPDataCmd(cmd,totalcode,scode,dindex,datas);
    }
    
    /**
     * 生成文件指令（附加文件指定位置的内容）
     * @param cmd
     * @param dindex
     * @param ufr
     * @param readPos
     * @return 
     */
    public static UDPDataCmd makeFileCmdWithFile(String cmd,long dindex,UDPFileReader ufr,long readPos) throws Exception
    {
        return makeFileCmd(cmd,ufr.getFileCode(),dindex,ufr.readPack(readPos));
    }
    
    /**
     * 
     * @param args 
     */
    public static void main(String[] args)
    {
        try {
//            int size = 500;
//            ByteBuffer bb = ByteBuffer.allocate(size);
//            bb.clear();
//            new FileInputStream("/home/wcss/视频/QvodMovies/1_报复老公出轨拍av边打电话边11.rmvb").getChannel().read(bb,size);
//            byte[] filecnt = bb.array();
//            System.out.println("长度：" + filecnt.length);
//            String totalCode = FileCodeMaker.makeFileCode(new File("/home/wcss/视频/QvodMovies/1_报复老公出轨拍av边打电话边11.rmvb"));
            File ff = new File("/home/wcss/视频/QvodMovies/1_报复老公出轨拍av边打电话边11.rmvb");
            UDPFileReader ufr = new UDPFileReader(ff);
            ufr.open();
            UDPDataCmd udc = UDPDataCmd.makeFileCmdWithFile("sendcontent", 999999, ufr, 2 * 500);
            String jsonstr = JSONObject.fromObject(udc).toString();
            System.out.println("指令原文：" + jsonstr);
            System.out.println("指令长度：" + jsonstr.getBytes().length);
            ufr.close();
        } catch (Exception ex) {
            Logger.getLogger(UDPDataCmd.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the cmd
     */
    public String getCmd() {
        return cmd;
    }

    /**
     * @param cmd the cmd to set
     */
    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    /**
     * @return the totalCode
     */
    public String getTotalCode() {
        return totalCode;
    }

    /**
     * @param totalCode the totalCode to set
     */
    public void setTotalCode(String totalCode) {
        this.totalCode = totalCode;
    }

    /**
     * @return the subCode
     */
    public String getSubCode() {
        return subCode;
    }

    /**
     * @param subCode the subCode to set
     */
    public void setSubCode(String subCode) {
        this.subCode = subCode;
    }

    /**
     * @return the dataIndex
     */
    public long getDataIndex() {
        return dataIndex;
    }

    /**
     * @param dataIndex the dataIndex to set
     */
    public void setDataIndex(long dataIndex) {
        this.dataIndex = dataIndex;
    }

    /**
     * @return the data
     */
    public String getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(String data) {
        this.data = data;
    }
    
}
